package com.koron.css2.projectCenter.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.impl.OpenUserAdd;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.projectCenter.bean.PjConsultBean;
import com.koron.css2.projectCenter.bean.PjMeterBean;
import com.koron.css2.projectCenter.dto.PjMeterSaveDto;
import com.koron.css2.projectCenter.mapper.PjConsultMapper;
import com.koron.css2.projectCenter.service.PjConsultService;
import com.koron.css2.projectCenter.vo.PjMeterVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.IDUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用水咨询-登记保存
 * @author 许海宜
 * 2020年3月6日
 */
public class PjConsultSave implements ServerInterface {
	
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<Map> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", Map.class);
		try {
			//将入参转换为Bean
			String data = JsonUtils.objectToJson(req.getData());
			JsonNode jsonNode = JsonUtils.stringToJsonNode(data);
			PjConsultBean pjConsultBean = JsonUtils.objectToPojo(jsonNode.get("consult"), PjConsultBean.class);
			CtmInfoBean ctmInfoBean = JsonUtils.objectToPojo(jsonNode.get("ctmInfo"), CtmInfoBean.class);
			AccountInfoBean accountInfoBean = JsonUtils.objectToPojo(jsonNode.get("accountInfo"), AccountInfoBean.class);
			PjMeterSaveDto meterDto = JsonUtils.objectToPojo(jsonNode.get("meterDto"), PjMeterSaveDto.class);
			List<PjMeterVO> userList = meterDto==null?null:meterDto.getUserInfo();
			String checkResult = null;
			if("1".equals(ctmInfoBean.getCertType())) {//校验身份证号码是否符合要求
				if(!IDUtils.isIDNumber(ctmInfoBean.getCertNo())) {
					return MessageBean.create(Constant.NOT_NULL, "身份证件号码不符合要求，请检查证件类型", null);
				};
			}
			PubAdminAreaMapper pmapper = factory.getMapper(PubAdminAreaMapper.class,"_default");
			String clientAddr=pmapper.selectFullNameById(ctmInfoBean.getClientAddr());
			if(StringUtils.isEmpty(clientAddr)) {
				ctmInfoBean.setClientAddr(null);
			}
			if(StringUtils.isNotEmpty(clientAddr) && StringUtils.isNotEmpty(ctmInfoBean.getClientAddrDetail())
					) {
				//客户地址结构化拼接
				ctmInfoBean.setCtmAddr(clientAddr+"->"+ctmInfoBean.getClientAddrDetail());
			}

			//判断主键是否为空，空的为添加操作，否则为修改操作
			if(StringUtils.isBlank(pjConsultBean.getId())){
				if(userList==null ||  userList.size()==0) {
					return MessageBean.create(Constant.NOT_NULL, "多水表至少要有一条数据", null);
				}
				//检查节点字段
				checkResult = checkNode(pjConsultBean, ctmInfoBean, accountInfoBean, "register");
				if(!"OK".equals(checkResult)) {
					return MessageBean.create(Constant.NOT_NULL, checkResult, null);
				}
				//保存客户
				saveCtm(factory, userInfo, ctmInfoBean);
				//保存账户
				saveAccount(factory, userInfo, accountInfoBean, ctmInfoBean.getCtmNo());
				//保存登记信息
				pjConsultBean.setUserCtmTempId(ctmInfoBean.getId());
				pjConsultBean.setUserAccountTempId(accountInfoBean.getId());
				String consultId = savePjConsult(factory, userInfo, pjConsultBean, PjConsultService.REGISTER_NODE);
				//返回ID
				meterDto.setConsultId(consultId);
				saveMeter(factory, userInfo,meterDto);//保存多水表
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", consultId);
				msg.setData(map);
				BaseReceiptAccessoryMapper mapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
				if(!StringUtils.isBlank(pjConsultBean.getTempId()))
					mapper.updateAccessoryReceiptId(consultId, pjConsultBean.getTempId());
			} else {
				//检查节点编号
				String nodeCode = pjConsultBean.getProcessState();
				if(StringUtils.isBlank(nodeCode)) {
					nodeCode = PjConsultService.REGISTER_NODE;
				}
				if(!StringUtils.equalsAny(nodeCode, PjConsultService.REGISTER_NODE, PjConsultService.SURVEY_NODE, PjConsultService.DESIGN_NODE, PjConsultService.BUDGET_NODE)) {
					return MessageBean.create(Constant.NOT_NULL, "流程状态不存在。", null);
				}
				//检查节点字段
				checkResult = checkNode(pjConsultBean, ctmInfoBean, accountInfoBean, nodeCode);
				if(!"OK".equals(checkResult)) {
					return MessageBean.create(Constant.NOT_NULL, checkResult, null);
				}
				//检查客户、账户修改标记
				if(jsonNode.get("updateCtmFlag") == null) {
					return MessageBean.create(Constant.NOT_NULL, "客户修改标记不能为空。", null);
				}
				if(jsonNode.get("updateAccountFlag") == null) {
					return MessageBean.create(Constant.NOT_NULL, "账户修改标记不能为空。", null);
				}
				String updateCtmFlag = jsonNode.get("updateCtmFlag").asText();
				String updateAccountFlag = jsonNode.get("updateAccountFlag").asText();
				//判断客户信息是否能修改
				if("1".equals(updateCtmFlag)) {
					//保存客户
					saveCtm(factory, userInfo, ctmInfoBean);
				}
				//判断账户信息是否能修改
				if("1".equals(updateAccountFlag)) {
					//保存客户、账户
					saveAccount(factory, userInfo, accountInfoBean, ctmInfoBean.getCtmNo());
				}
				//保存用水咨询
				savePjConsult(factory, userInfo, pjConsultBean, nodeCode);
				//登记节点保存多水表
				if(nodeCode == PjConsultService.REGISTER_NODE) {
					meterDto.setConsultId(pjConsultBean.getId());
					saveMeter(factory, userInfo,meterDto);
				}
			}
			//输出执行结果
			return msg;
		} catch (Exception e) {
			factory.close(false);
			logger.error("用水咨询保存失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}
	
	/**
	 * 保存用水咨询
	 * @param factory
	 * @param userInfo
	 * @param pjConsultBean
	 */
	public String savePjConsult(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean, String nodeCode) {
		String consultId = null;
		//登记节点
		if(StringUtils.equalsAny(nodeCode, PjConsultService.REGISTER_NODE, PjConsultService.SURVEY_NODE, PjConsultService.DESIGN_NODE)) {
			consultId = saveRegister(factory, userInfo, pjConsultBean);
		}
		//勘察节点
		if(StringUtils.equalsAny(nodeCode, PjConsultService.SURVEY_NODE, PjConsultService.DESIGN_NODE)) {
			saveSurvey(factory, userInfo, pjConsultBean);
		}
		//设计节点
		if(StringUtils.equals(nodeCode, PjConsultService.DESIGN_NODE)) {
			saveDesign(factory, userInfo, pjConsultBean);
		}
		//预算节点
		if(StringUtils.equals(nodeCode, PjConsultService.BUDGET_NODE)) {
			saveBudget(factory, userInfo, pjConsultBean);
		}
		return consultId;
	}

	/**
	 * 保存登记信息
	 * @param factory
	 * @param userInfo
	 * @param pjConsultBean
	 */
	public String saveRegister(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean) {
		String consultId = "";
		if(StringUtils.isBlank(pjConsultBean.getId())){
			//新增
			consultId = Tools.getObjectId();
			pjConsultBean.setId(consultId);
			pjConsultBean.setBillNo(Tools.getBillNo(factory, "PJ_CONSULT"));//统一编号生成方法
			pjConsultBean.setBillDate(CommonUtils.getCurrentDate());
			pjConsultBean.setTenantId(userInfo.getCurWaterCode());
			pjConsultBean.setCreateName(userInfo.getUserInfo().getName());
			pjConsultBean.setCreateAccount(userInfo.getUserInfo().getAcount());
			factory.getMapper(PjConsultMapper.class).addRegister(pjConsultBean);
		} else {
			//修改
			pjConsultBean.setUpdateName(userInfo.getUserInfo().getName());
			pjConsultBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
			factory.getMapper(PjConsultMapper.class).updateRegister(pjConsultBean);
		}
		return consultId;
	}
	
	/**
	 * 保存勘察
	 * @param factory
	 * @param userInfo
	 * @param pjConsultBean
	 */
	public void saveSurvey(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean) {
		pjConsultBean.setUpdateName(userInfo.getUserInfo().getName());
		pjConsultBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
		factory.getMapper(PjConsultMapper.class).updateSurvey(pjConsultBean);
	}
	
	/**
	 * 保存设计
	 * @param factory
	 * @param userInfo
	 * @param pjConsultBean
	 */
	public void saveDesign(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean) {
		pjConsultBean.setUpdateName(userInfo.getUserInfo().getName());
		pjConsultBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
		factory.getMapper(PjConsultMapper.class).updateDesign(pjConsultBean);
	}
	
	/**
	 * 保存预算
	 * @param factory
	 * @param userInfo
	 * @param pjConsultBean
	 */
	public void saveBudget(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean) {
		pjConsultBean.setUpdateName(userInfo.getUserInfo().getName());
		pjConsultBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
		if(pjConsultBean.getBudgetMoney()==null || pjConsultBean.getBudgetMoney()==0) {
			pjConsultBean.setBudgetMoney(
					(pjConsultBean.getBudgetLaborMeterAfter()==null?0:pjConsultBean.getBudgetLaborMeterAfter()) +
					(pjConsultBean.getBudgetLaborMeterBefore()==null?0:pjConsultBean.getBudgetLaborMeterBefore())+
					(pjConsultBean.getBudgetMatrMeterAfter()==null?0:pjConsultBean.getBudgetMatrMeterAfter())+
					(pjConsultBean.getBudgetMatrMeterBefore()==null?0:pjConsultBean.getBudgetMatrMeterBefore())+
					(pjConsultBean.getBudgetOther()==null?0:pjConsultBean.getBudgetOther())+
					(pjConsultBean.getDesignCost()==null?0:pjConsultBean.getDesignCost())
					);
		}
		factory.getMapper(PjConsultMapper.class).updateBudget(pjConsultBean);
	}

	/**
	 * 保存客户信息
	 * @param factory
	 * @param userInfo
	 * @param ctmInfoBean
	 */
	public void saveCtm(SessionFactory factory, UserInfoBean userInfo, CtmInfoBean ctmInfoBean) {
		if(StringUtils.isBlank(ctmInfoBean.getId())){
			//新增
			ctmInfoBean.setId(Tools.getObjectId());
			ctmInfoBean.setCreateName(userInfo.getUserInfo().getName());
			ctmInfoBean.setCreateAccount(userInfo.getUserInfo().getAcount());
			ctmInfoBean.setCtmNo(Tools.getBillNo(factory, "USER_CTM", userInfo.getCurWaterCode()));
			factory.getMapper(CtmInfoMapper.class).ctmInfoAdd(ctmInfoBean, 2);
		} else {
			//修改
			ctmInfoBean.setUpdateName(userInfo.getUserInfo().getName());
			ctmInfoBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
			factory.getMapper(CtmInfoMapper.class).ctmInfoUpdate(ctmInfoBean, 2);
		}
	}

	/**
	 * 保存账户信息
	 * @param factory
	 * @param userInfo
	 * @param accountInfoBean
	 * @param ctmNo
	 */
	public void saveAccount(SessionFactory factory, UserInfoBean userInfo, AccountInfoBean accountInfoBean, String ctmNo) {
		if(StringUtils.isBlank(accountInfoBean.getId())){
			//新增
			accountInfoBean.setId(Tools.getObjectId());
			accountInfoBean.setCreateName(userInfo.getUserInfo().getName());
			accountInfoBean.setCreateAccount(userInfo.getUserInfo().getAcount());
			accountInfoBean.setAccountNo(Tools.getBillNo(factory, "USER_ACCOUNT", userInfo.getCurWaterCode()));
			factory.getMapper(AccountInfoMapper.class).accountInfoAdd(accountInfoBean, 2);
		} else {
			//修改
			accountInfoBean.setUpdateName(userInfo.getUserInfo().getName());
			accountInfoBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
			factory.getMapper(AccountInfoMapper.class).accountInfoUpdate(accountInfoBean, 2);
		}
	}
	
	/**
	 * 检查登记节点
	 * @param pjConsultBean
	 * @param ctmInfoBean
	 * @param accountInfoBean
	 * @return
	 */
	public String checkNode(PjConsultBean pjConsultBean, CtmInfoBean ctmInfoBean, AccountInfoBean accountInfoBean, String nodeCode) {
		String checkResult = "";
		//登记节点
		if(StringUtils.equalsAny(nodeCode, PjConsultService.REGISTER_NODE, PjConsultService.SURVEY_NODE, PjConsultService.DESIGN_NODE)) {
			checkResult = checkCtm(ctmInfoBean);
			if(!"OK".equals(checkResult)){
				return checkResult;
			}
			checkResult = checkRegister(pjConsultBean);
			if(!"OK".equals(checkResult)){
				return checkResult;
			}
			checkResult = checkAccount(accountInfoBean);
			if(!"OK".equals(checkResult)){
				return checkResult;
			}
		}
		//主键字段
		if(StringUtils.equalsAny(nodeCode, PjConsultService.SURVEY_NODE, PjConsultService.DESIGN_NODE, PjConsultService.BUDGET_NODE)) {
			checkResult = checkPrimaryKey(pjConsultBean, ctmInfoBean, accountInfoBean);
			if(!"OK".equals(checkResult)){
				return checkResult;
			}
		}
		//勘察节点
		if(StringUtils.equalsAny(nodeCode, PjConsultService.SURVEY_NODE, PjConsultService.DESIGN_NODE)) {
			checkResult = checkSurvey(pjConsultBean);
			if(!"OK".equals(checkResult)){
				return checkResult;
			}
		}
		//设计节点
		if(StringUtils.equals(nodeCode, PjConsultService.DESIGN_NODE)) {
			checkResult = checkDesign(pjConsultBean);
			if(!"OK".equals(checkResult)){
				return checkResult;
			}
		}
		//预算节点
		if(StringUtils.equals(nodeCode, PjConsultService.BUDGET_NODE)) {
			checkResult = checkBudget(pjConsultBean);
			if(!"OK".equals(checkResult)){
				return checkResult;
			}
		}
		return "OK";
	}
	
	/**
	 * 主键字段检查
	 * @param pjConsultBean
	 * @param ctmInfoBean
	 * @param accountInfoBean
	 * @return
	 */
	public String checkPrimaryKey(PjConsultBean pjConsultBean, CtmInfoBean ctmInfoBean, AccountInfoBean accountInfoBean) {
		if(StringUtils.isBlank(pjConsultBean.getId())){
			return "主键不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getUserCtmTempId()) || StringUtils.isBlank(ctmInfoBean.getId())){
			return "客户主键不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getUserAccountTempId()) || StringUtils.isBlank(accountInfoBean.getId())){
			return "账户主键不能为空。";
		}
		return "OK";
	}

	/**
	 * 登记字段检查
	 * @param pjConsultBean
	 * @return
	 */
	public String checkRegister(PjConsultBean pjConsultBean) {
		if(StringUtils.isBlank(pjConsultBean.getAgentLinkTel())){
			return "经办人联系电话不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getPjType())){
			return "工程类型不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getUrgentFlag()+"")){
			return "紧急程度不能为空。";
		}
		//else if(StringUtils.isBlank(pjConsultBean.getAdminArea())) {
		//	return "行政区域不能为空。";
		//}else if(StringUtils.isBlank(pjConsultBean.getBusinessAbode())) {
			//return "营业所不能为空。";
		//}
		else if(StringUtils.isBlank(pjConsultBean.getBusinessArea())) {
			return "营业区域不能为空。";
		}
		return "OK";
	}

	/**
	 * 勘察字段检查
	 * @param pjConsultBean
	 * @return
	 */
	public String checkSurvey(PjConsultBean pjConsultBean) {
		if(StringUtils.isBlank(pjConsultBean.getSurveyStaff())){
			return "勘察人员不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getSurveyDate())){
			return "勘察日期不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getSurveyResult())){
			return "勘察结论不能为空。";
		}
		return "OK";
	}

	/**
	 * 设计字段检查
	 * @param pjConsultBean
	 * @return
	 */
	public String checkDesign(PjConsultBean pjConsultBean) {
		if(StringUtils.isBlank(pjConsultBean.getDesignStaff())){
			return "设计人员不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getDesignDate())){
			return "设计日期不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getDesignDesc())){
			//return "设计说明不能为空。";
		}
		return "OK";
	}

	/**
	 * 预算字段检查
	 * @param pjConsultBean
	 * @return
	 */
	public String checkBudget(PjConsultBean pjConsultBean) {
		if(pjConsultBean.getBudgetMoney() == null && 
				(
					(
						(pjConsultBean.getBudgetLaborMeterAfter()==null?0:pjConsultBean.getBudgetLaborMeterAfter()) +
						(pjConsultBean.getBudgetLaborMeterBefore()==null?0:pjConsultBean.getBudgetLaborMeterBefore())+
						(pjConsultBean.getBudgetMatrMeterAfter()==null?0:pjConsultBean.getBudgetMatrMeterAfter())+
						(pjConsultBean.getBudgetMatrMeterBefore()==null?0:pjConsultBean.getBudgetMatrMeterBefore())+
						(pjConsultBean.getBudgetOther()==null?0:pjConsultBean.getBudgetOther())+
						(pjConsultBean.getDesignCost()==null?0:pjConsultBean.getDesignCost())
					) == 0				
				)
		){
			return "预算合计不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getPrepayType())){
			return "结算方式不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getBudgetStaff())){
			return "编制人员不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getBudgetDate())){
			return "编制日期不能为空。";
		//}else if(pjConsultBean.getDepositMoney()==null){
		//	return "押金不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getBudgetComments())){
			return "编制说明不能为空。";
		}else if(StringUtils.isBlank(pjConsultBean.getProjectCharge())){
			return "工程收款主体不能为空。";
		}
		return "OK";
	}

	/**
	 * 客户字段检查
	 * @param ctmInfoBean
	 * @return
	 */
	public String checkCtm(CtmInfoBean ctmInfoBean) {
		if(StringUtils.isBlank(ctmInfoBean.getCtmType())){
			return "客户类型不能为空。";
		}else if(StringUtils.isBlank(ctmInfoBean.getCtmName())){
			return "业主名称不能为空。";
		}else if(StringUtils.isBlank(ctmInfoBean.getCertType())){
			return "业主证件类型不能为空。";
		}else if(StringUtils.isBlank(ctmInfoBean.getCertNo())){
			return "业主证件号码不能为空。";
		}
		return "OK";
	}

	/**
	 * 账户字段检查
	 * @param accountInfoBean
	 * @return
	 */
	public String checkAccount(AccountInfoBean accountInfoBean) {
		if(StringUtils.isBlank(accountInfoBean.getPayWay())){
			return "缴费方式不能为空。";
		}else if(StringUtils.isBlank(accountInfoBean.getAccountType())){
			return "账户类型不能为空。";
		}
		/*else if(StringUtils.isBlank(accountInfoBean.getInvoiceType())){
			return "发票类型不能为空。";
		}*/
		return "OK";
	}
	public void saveMeter(SessionFactory factory, UserInfoBean userInfo, PjMeterSaveDto dto) {
		//PjMeterSaveDto dto = JsonUtils.objectToPojo(req.getData(), PjMeterSaveDto.class);
		//TODO 字段校验
		
		//检查用水咨询是否存在
		PjConsultMapper pjConsultMapper = factory.getMapper(PjConsultMapper.class);
		PjConsultBean pjConsultBean = pjConsultMapper.selectById(dto.getConsultId());
		CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
		//查询临时表
		CtmInfoBean ctmInfoBean = ctmMapper.getCtmInfoById(pjConsultBean.getUserCtmTempId(), 2);
		//删除关联关系
		pjConsultMapper.delMeter(dto.getConsultId());
		//获取Mapper实例
		UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
		String userId = "";//用户ID
		int installAmount = 0;
		for (PjMeterVO user : dto.getUserInfo()) {
			//判断用户主键是否为空
			if(StringUtils.isBlank(user.getId())) {
				//新增
				userId = Tools.getObjectId();
				user.setId(userId);
				user.setBusinessAbode(pjConsultBean.getBusinessAbode());
				user.setBusinessArea(pjConsultBean.getBusinessArea());
				user.setAdminArea(pjConsultBean.getAdminArea());
				user.setUseTimeLimit(pjConsultBean.getLimitDate());
				user.setStatus("1");
				user.setCreateAccount(userInfo.getUserInfo().getAcount());
				user.setCreateName(userInfo.getUserInfo().getName());
				user.setTenantId(userInfo.getCurWaterCode());
				user.setUserNo(Tools.getBillNo(factory, "USER_INFO", userInfo.getCurWaterCode()));
				//判断户表改造
				if(pjConsultBean.getPjType().equals("2")) {
					user.setReformFlag(1);
				}
				OpenUserAdd.ctmInfoToUserInfo(ctmInfoBean,user);
				userInfoMapper.userInfoAdd(user, 2);
			} else {
				//修改
				userId = user.getId();
				user.setBusinessAbode(pjConsultBean.getBusinessAbode());
				user.setBusinessArea(pjConsultBean.getBusinessArea());
				user.setAdminArea(pjConsultBean.getAdminArea());
				user.setUseTimeLimit(pjConsultBean.getLimitDate());

				user.setUpdateAccount(userInfo.getUserInfo().getAcount());
				user.setUpdateTime(CommonUtils.getCurrentDate());
				user.setStatus("1");
				OpenUserAdd.ctmInfoToUserInfo(ctmInfoBean,user);
				userInfoMapper.userInfoUpdate(user, 2);
			}
			//添加关联关系
			PjMeterBean pjMeterBean = new PjMeterBean();
			pjMeterBean.setId(Tools.getObjectId());
			pjMeterBean.setConsultId(dto.getConsultId());
			pjMeterBean.setUserInfoTempId(userId);
			pjMeterBean.setInflate(user.getInflate());
			pjMeterBean.setSecondaryWater(user.getSecondaryWater());
			pjConsultMapper.addMeter(pjMeterBean);
			installAmount++;
		}
		//更新单据表的装表数量
		pjConsultMapper.updateInstallAmount(pjConsultBean.getId(), installAmount);
	}
}
